#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Text;
using Pandora;
using UnityEditor;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

namespace PandoraEditor
{
    public class PdrWindowHelper
    {
        #region Assets
        /// <summary>
        /// 修改UIPanel
        /// </summary>
        /// <param name="title"></param>
        /// <param name="action"></param>
        public static void ModifyUIPrefabs(string title, PdrFindBase.GameObjectDelegate action)
        {
            ExecuteAssetObject<GameObject>(title, PdrFindHelper.GetUIPrefabs(), source =>
            {
                GameObject newGo = PrefabUtility.InstantiatePrefab(source.gameObject) as GameObject;
                if (action(newGo))
                {
                    PrefabUtility.SaveAsPrefabAsset(newGo, AssetDatabase.GetAssetPath(source.gameObject));
                }
                Object.DestroyImmediate(newGo);
            });
        }
        /// <summary>
        /// 检测所有Prefab
        /// </summary>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckAllPrefabs(string title, params Action<AssetObject>[] actions)
        {
            ExecuteAssetObject<GameObject>(title, PdrFindHelper.GetAllPrefabs(), actions);
        }
        /// <summary>
        /// 检测所有Prefab
        /// </summary>
        /// <param name="root"></param>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckAllPrefabs(string root, string title, params Action<AssetObject>[] actions)
        {
            ExecuteAssetObject<GameObject>(title, PdrFindBase.GetPrefabs(root), actions);
        }
        /// <summary>
        /// 检测UIPrefab
        /// </summary>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckUIPrefabs(string title, params Action<AssetObject>[] actions)
        {
            ExecuteAssetObject<GameObject>(title, PdrFindHelper.GetUIPrefabs(), actions);
        }
        /// <summary>
        /// 检测所有Texture
        /// </summary>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckAllTexture(string title, params Action<AssetObject>[] actions)
        {
            ExecuteAssetObject<Texture>(title, PdrFindHelper.GetAllTextures(), actions);
        }
        /// <summary>
        /// 检测指定Fbx
        /// </summary>
        /// <param name="root"></param>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckAllFbx(string root, string title, params Action<AssetObject>[] actions)
        {
            ExecuteAssetObject<AssetImporter>(title, PdrFindBase.GetFbxImporters(root), actions);
        }
        #endregion
        #region Art
        /// <summary>
        /// 执行ArtTextureImporter
        /// </summary>
        /// <param name="title"></param>
        /// <param name="action"></param>
        public static void CheckArtTextures(string title, Action<AssetObject> action)
        {
            ExecuteAssetObject<Texture>(title, PdrFindHelper.GetArtTextures(), action);
        }
        /// <summary>
        /// 执行ArtFBX模型
        /// </summary>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckArtFBXModel(string title, params Action<AssetObject>[] actions)
        {
            ExecuteAssetObject<AssetImporter>(title, PdrFindHelper.GetFbxModels(), actions);
        }
        /// <summary>
        /// 执行ArtTextureImporter
        /// </summary>
        /// <param name="title"></param>
        /// <param name="action"></param>
        public static void CheckAtlasTextures(string title, Action<AssetObject>[] action)
        {
            ExecuteAssetObject<Texture>(title, PdrFindHelper. GetAtlasTextures(), action);
        }
        /// <summary>
        /// 检测UISprite
        /// </summary>
        /// <param name="title"></param>
        /// <param name="actions"></param>
        public static void CheckUISpritesName(string title, params Action<AssetObject>[] actions)
        {
            EditorUtility.DisplayProgressBar(title, "Please Wait......", 0);
            ClearConsole();
            AssetDatabase.Refresh();
            List<string> list = PdrFindHelper.GetSourceSprites();
            for (int i = 0; i < list.Count; i++)
            {
                var firstSprite = AssetObject.Create<Sprite>(list[i]);
                StringBuilder sb = new StringBuilder();
                sb.Append(firstSprite.name).Append("(").Append(i + 1).Append("/").Append(list.Count).Append(")");
                EditorUtility.DisplayProgressBar(title, sb.ToString(), i * 1.0f / list.Count);
                for (int j = i + 1; j < list.Count; j++)
                {
                    var lastSprite = AssetObject.Create<Sprite>(list[j]);
                    if (firstSprite.name == lastSprite.name)
                    {
                        foreach (var action in actions)
                        {
                            action(firstSprite);
                            action(lastSprite);
                        }
                    }
                }
                if (firstSprite.name.IndexOf(' ') != -1 || firstSprite.name.IndexOf("..", StringComparison.Ordinal) != -1)
                {
                    foreach (var action in actions)
                    {
                        action(firstSprite);
                    }
                }
            }
            EditorUtility.ClearProgressBar();
        }
        #endregion
        #region Excute Base
        private static void ExecuteAssetObject<T>(string title, List<string> list, params Action<AssetObject>[] actions)
        {
            EditorUtility.DisplayProgressBar(title, "Please Wait......", 0);
            ClearConsole();
            for (int i = 0; i < list.Count; i++)
            {
                var source = AssetObject.Create<T>(list[i]);
                StringBuilder sb = new StringBuilder();
                sb.Append(source.name).Append("(").Append(i + 1).Append("/").Append(list.Count).Append(")");
                EditorUtility.DisplayProgressBar(title, sb.ToString(), i * 1.0f / list.Count);
                foreach (var action in actions)
                {
                    action(source);
                }
            }
            EditorUtility.ClearProgressBar();
        }
        public static void ClearConsole()
        {
            var logEntries = Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
            System.Diagnostics.Debug.Assert(logEntries != null, "logEntries != null");
            //            var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
            //            clearMethod.Invoke(null, null);
        }
        #endregion
        #region Other
        /// <summary>
        /// Finds the game object in current scene.
        /// </summary>
        /// <param name="action">Action.</param>
        public static void FindGameObjectInCurrentScene(Action<GameObject> action)
        {
            Scene scene = SceneManager.GetActiveScene();
            GameObject[] arrGO = scene.GetRootGameObjects();
            if (arrGO != null && arrGO.Length > 0)
            {
                for (int i = 0; i < arrGO.Length; i++)
                {
                    action(arrGO[i]);
                }
            }
        }
        #endregion
    }
}